/*
 * EPRO SYSTEMS (S) PTE LTD Copyright 2011, All rights reserved.
 *  
 * 修订记录: 
 * 2011-10-28 [你的名称] 新增代码
 */
package com.yu.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串帮助类
 * 
 * @author Administrator
 * 
 */
public final class StringUtil {
	private StringUtil() {
	}
	
	 public static class Ip {
	    public boolean isIP(String addr) {
	      if(addr.length() < 7 || addr.length() > 15 || "".equals(addr)) {
	         return false;
	      }
	      
	      /**
	       * 判断IP格式和范围
	       */
	      String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
	      Pattern pat = Pattern.compile(rexp);  
	      Matcher mat = pat.matcher(addr);  
	      return mat.find();
	    }
	  }

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return null == str || "".equals(str.trim()) || "null".equals(str.trim());
	}

	/**
	 * 判断字符是否数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isDigital(String str) {
		if (isEmpty(str)) {
			return false;
		}
		return str.matches("(-)?\\d+");
	}

	/**
	 * 判断字符是否数字
	 * 
	 * @param str
	 * @return
	 */
	public static String trim(String str) {
		return str.trim();
	}

	/**
	 * 截取指定长度的字符串
	 * 
	 * @param sourceString
	 * @param length
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String getSubString(String sourceString, int length) {
		if (sourceString.length() > length) {
			sourceString = sourceString.substring(0, length);
		}

		return sourceString;
	}

}
